package com.etc.service.impl;

import com.etc.dao.CinemaDao;
import com.etc.dao.RecordDao;
import com.etc.dao.UnpaidDao;
import com.etc.dao.UserRecordDao;
import com.etc.entity.Unpaid;
import com.etc.entity.UserRecord;
import com.etc.entity.UserRecordFinall;
import com.etc.service.MovieService;
import com.etc.service.UserRecordService;
import com.etc.util.DateUtil;
import com.etc.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by dzq on 2019/5/24.
 * 前台页面显示的结果集
 */
@Service
public class UserRecordServiceImpl implements UserRecordService {
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private MovieService movieService;
    @Autowired
    private CinemaDao cinemaDao;
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private DateUtil dateUtil;
    @Autowired
    private UnpaidDao unpaidDao;

    @Override
    public List<UserRecordFinall> FindUserRecord(Integer userid) {
        String remark = "已支付";
        Date movieDate;
        List<UserRecordFinall> userRecordFinalls = new ArrayList<UserRecordFinall>();

        //查询未支付的订单
        List<Unpaid> unpaids = unpaidDao.findAll();
        UserRecord userRecord = new UserRecord();
        //通过工具类把查询结果映射到实体类上
        List<UserRecord> userRecords = EntityUtils.castEntity(userRecordDao.FindAllRecordByUserWithBefore(userid), UserRecord.class, userRecord);
        //通过循环把最终结果整合成一个list集合并返回
        userRecords.forEach(System.out::println);
        unpaids.forEach(System.out::println);
        for (int index = 0; index < userRecords.size(); index++) {
            //获取毫秒差
            for (int i = 0; i < unpaids.size(); i++) {

                if (unpaids.get(i).getRid() == userRecords.get(index).getRid()) {
                    System.out.println("userRecords.get(index).getRid():"+userRecords.get(index).getRid()+"____-----");
                    if (unpaids.get(i).getPlacetime().getTime() + 10 * 60 * 1000 - new Date().getTime() < 0) {
                        System.out.println("userRecords.get(index).getRid():"+userRecords.get(index).getRid()+"*********");
                        remark="0";
                        break;

                    }else  {
                        System.out.println("userRecords.get(index).getRid():"+userRecords.get(index).getRid()+"$$$$$$$");
                        remark = Long.toString(unpaids.get(i).getPlacetime().getTime() + 10 * 60 * 1000 - new Date().getTime());
                        break;
                    }

                }else {
                    remark="1";

                }
            }
            UserRecordFinall userRecordFinall = new UserRecordFinall(userRecords.get(index), userRecordDao.FindSeatsByRid(userRecords.get(index).getRid()), remark);
            userRecordFinalls.add(userRecordFinall);
        }
        return userRecordFinalls;
    }

    @Override
    public Unpaid AddUnpaid(Unpaid unpaid) {
        return unpaidDao.save(unpaid);
    }

    @Override
    public void DeleteUnpaid(Integer uid) {
        unpaidDao.delete(uid);
    }

    @Override
    public void DeleteUnpaidForRid(Integer rid) {
        unpaidDao.delete(unpaidDao.findByRidForDel(rid).getUid());
    }


}
